<template>
	<div class="wrap" v-loading="loading" element-loading-text="等下,我正在请求" element-loading-spinner="el-icon-loading">
		<basic-opt :has_left="false">
			<template #add>
				<div class="opt-item">
					<el-button @click="addForm" icon="el-icon-circle-plus-outline" type="primary" size="medium">新增人脸组
					</el-button>
				</div>
			</template>
		</basic-opt>

		<div class="table-box">
			<el-table :data="data" stripe :header-cell-style="header_cell_style" :max-height="table_max_height">
				<el-table-column prop="id" label="ID"></el-table-column>
				<el-table-column prop="group_name" label="人脸组"></el-table-column>
				<el-table-column prop="group_id" label="人脸组ID"></el-table-column>
				<el-table-column prop="group.address.address" label="地址"></el-table-column>
				<el-table-column label="操作" width="200">
					<template #default="scope">
						<el-button @click="onDelData(scope.row)" type="danger" plain size="mini">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<!-- <page-component @pageChanged="onPageChanged"></page-component> -->


		<!-- 添加人脸组 -->
		<compoment-drawer :drawer_form="drawer_form" :drawer_name="drawer_name" @getDrawerForm="getDrawerForm">
			<el-form ref="form" :model="form" label-width="120px">
				<el-scrollbar v-loading="form_loading" element-loading-spinner="el-icon-loading"
					element-loading-text="正在进行请求,请稍后" height="80vh">
					<div class="form-flex">
						<el-form-item label="搜索地址">
							<address-name :address_name="address_name" @getAddress="getAddress"></address-name>
						</el-form-item>
						<el-form-item label="选择人脸组" prop="groups">
							<el-select style="w-ipt" v-model="form.groups" @change="faceGroupChanged" multiple
								placeholder="请选择人脸组" size="medium">
								<el-option v-for="(item,index) in faceGroupList" :key="index" :label="item.group_name"
									:value="item.group_name">
								</el-option>
							</el-select>
						</el-form-item>
					</div>
				</el-scrollbar>
				<submit-reset @getChildNewForm="getNewForm" @getChildResetForm="resetForm('form')">
				</submit-reset>
			</el-form>
		</compoment-drawer>

		<!-- 删除数据确认 -->
		<delete-data :del_data_dialog="del_data_dialog" @getDelData="getDelData" @getCancelDelData="getCancelDelData">
			<el-descriptions class="margin-top" :column="1" size="medium" border>
				<el-descriptions-item>
					<template #label>
						id
					</template>
					{{ delData.id }}
				</el-descriptions-item>
				<el-descriptions-item>
					<template #label>
						人脸组名
					</template>
					{{ delData.group_name }}
				</el-descriptions-item>
				<el-descriptions-item>
					<template #label>
						人脸组Id
					</template>
					{{ delData.group_id }}
				</el-descriptions-item>
			</el-descriptions>
		</delete-data>
	</div>
	<div class="mobile-wrap">
		<div class="moblie-table-box">
			<el-table :data="data" stripe :header-cell-style="header_cell_style" :max-height="mobile_table_max_height">
				<el-table :data="data" stripe :header-cell-style="header_cell_style" :max-height="table_max_height">
					<el-table-column prop="id" label="ID"></el-table-column>
					<el-table-column prop="group_name" label="人脸组"></el-table-column>
					<el-table-column prop="group_id" label="人脸组ID"></el-table-column>
					<el-table-column prop="group.address.address" label="地址"></el-table-column>
				</el-table>
			</el-table>
		</div>
		<!-- <page-component @pageChanged="onPageChanged"></page-component> -->
	</div>
</template>

<script>
	import API from '@/api/index.js'
	import mixin from '@/mixin.js'
	import AddressName from '@/components/address/address-name.vue'

	export default {
		mixins: [mixin],
		components: {
			AddressName
		},
		data() {
			return {
				// 表单
				drawer_name: '添加人脸组',
				form_loading: false, // 表单填写时加载
				form: {
					device_id: '',
					groups: []
				},

				address_name: '',
				requestParma: {
					device_id: ''
				},
				faceGroupList: []
			}
		},
		created() {
			this.requestParma.device_id = localStorage.getItem('device_id');
			this.form.device_id = localStorage.getItem('device_id');
		},
		mounted() {
			this.getData();
		},
		methods: {
			async getData() {
				let self = this;
				self.loading = true;
				try {
					let params = {
						device_id: self.requestParma.device_id
					}
					self.addPageInfo(params)
					let res = await API.deviceGroups(params.device_id)
					self.loading = false;
					self.onGotPageData(res)
				} catch (err) {
					self.loading = false;
				}
			},
			// 确定删除数据
			submitDeleteData(val) {
				let self = this;
				API.delDeviceGroup(val).then(res => {
					console.log(res)
					self.getData();
					self.delSuccess();
				}).catch(err => {
					self.$message.error(err.response.data.message)
				})
			},
			// 获取地址
			getAddress(val) {
				let self = this;
				self.getFaceGroup(val.addressData.id);
			},
			// 获取人脸组
			getFaceGroup(val) {
				let self = this;
				API.addressGroups(val).then(res => {
					let data = res.data.data;
					self.faceGroupList = data;
				})
			},

			faceGroupChanged(val) {
				console.log(val);
			},
			// 提交表单
			getNewForm() {
				let self = this;
				self.form_loading = true;
				API.deviceGroup(self.form).then(res => {
					console.log(res)
					self.submitSuccess();
					self.drawer_form = false;
					self.form_loading = false;
					self.getData();
					self.initForm();
				}).catch(err => {
					self.form_loading = false;
					self.$message.error(err.response.data.message);
				});
			},
			initForm() {
				let self = this;
				self.address_name = '';
				self.form = {
					device_id: localStorage.getItem('device_id'),
					groups: []
				}
			},
		}
	}
</script>

<style scoped>

</style>
